Given(/^(?:|I) (?:am on|visit) the "([^"]*)"$/) do |page_name|
  visit to_page(page_name)
end
When(/^I click on "([^"]*)"(?: button|)$/) do |button|
  click_on(button)
end
When(/^I enter "([^"]*)" into "([^"]*)"$/) do |text, element|
  fill_in element,:with =>text
end
Then(/^I should see "([^"]*)"$/) do |text|
  page.should have_text(text),"error"
end

#We could put dummy parameters
When(/^I should see following (?:|.+?)(addresses|links)/) do |dummy_para,table|
  table.hashes.each do |row|
    links = row['links']
    page.should have_text (links)
  end
end
Then(/^I should see page title "([^"]*)"$/) do |page_title|
  if page.respond_to? :should
    page.should have_title(page_title)
  end
end
When(/^I should not see "([^"]*)"$/) do |text_I_dont_want_to_see|
  if page.respond_to? :should
  page.should have_no_content(text_I_dont_want_to_see)
  else
  assert page.has_no_content?(text_I_dont_want_to_see)
  end
end
When(/^I should see page header "([^"]*)" within "([^"]*)"$/) do |page_header, page_name|
  page_name.should have_content(page_header)
end
When(/^I should see link "([^"]*)" points to "([^"]*)"$/) do |link_name, url|
  pending
end
#Check links exist and point to proper urls.  Both methods are OK.
Then(/^I should see below "([^"]*)" point to "([^"]*)"$/) do |link_name, url|
  page.should have_link(link_name, {:href => url})
  #page.has_link?(link_name, {:href => url})
end
Then(/^I should see "([^"]*)" within "([^"]*)"$/) do |text, component|
  within(to_css_locator(component)) do
      page.should have_text(text)
  end
end
When(/^I click on "([^"]*)" auto-complete option$/) do |link_text|
  within(".pac-container") do
  page.has_css?(".pac-item", :text => link_text)
  page.find(".pac-item", :text => link_text).click
end
end

Then /^I should see "([^"]*)" in the "([^"]*)" field$/ do |value, field|
   #puts value
  if is_an_address_string? value
    #This is a work around Google's data change issue.
    #output (Dr or Drive)
    all_street_types_with_brackets = all_street_types_with_brackets_from value
    #output Dr, Drive
    all_street_types = all_street_types_from value

    all_street_types.map do |type|
      new_value = value.gsub("#{all_street_types_with_brackets}", type)
      puts new_value
      page.find_field(field).value == new_value
      #page.find_field(field).value.should eql(new_value)
    end.include?(true).should be_true
  else
    # below two methods are all OK
    #page.find_field(field).value == value
    page.find_field(field).value.should eql(value)
  end
end


Then /^I should not see the map$/ do
    page.should have_css("#mapCanvas", visible: false)
  end
When(/^I should see "([^"]*)" (?:next to information box)$/) do |element|
  case element
    when "NBN Rollout Map"
      page.should have_css(".messagePromo")
  end
end

=begin
Then(/^redirect to new URL$/) do


    require 'nokogiri'
    file=File.open("redirectRules.xml")
    url_data = []
    doc = Nokogiri::XML(file)

    doc.xpath("//nbnco:redirectRule").each do |u|
      from_url = u.at("./nbnco:fromUrl").inner_text
      to_url = u.at("./nbnco:toUrl").inner_text


      combined_url = "http://www.nbnco.com.au" + from_url
      expected_url =  "http://www.nbnco.com.au" + to_url
      visit combined_url

    if current_url.should == expected_url
       puts 'PASSED'
       puts "from_url is #{combined_url}"
    else
      puts 'FAILED'
      puts "from_url is #{combined_url}"
    end
    end
  end
=end

Then(/^redirect to new URL$/) do
  file=File.open("redirectRules.xml")
  url_data = []
  doc = Nokogiri::XML(file)

  doc.xpath("//nbnco:redirectRule").each do |u|
    from_url = u.at("./nbnco:fromUrl").inner_text
    to_url = u.at("./nbnco:toUrl").inner_text
    combined_url = "http://www.nbnco.com.au" + from_url

    RestClient.get(combined_url) do |response, request, result, &body|
      response.headers[:location].should == to_url
      puts("#########")
      puts("request send to #{combined_url}")
      puts("redirected to http://www.nbnco.com.au#{response.headers[:location]}")
      puts("with response code: #{response.code}")

    end
  end
end
